package com.bcx.crm.java.proxy.jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by ys on 2018/1/18.
 */
public class ProxyFactory {
    //维护一个目标对象
    private Object target;
    public ProxyFactory(Object target){
        this.target=target;
    }
    //给目标对象生成代理对象
    public Object getProxyInstance(){
        Object o = Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("-----------开启事务2---");
                        Object returnValue = method.invoke(target, args);
                        System.out.println("---提交事务2---");
                        return returnValue;
                    }
                });
        return o;
    }
    /**
     * object.getClass().getInterfaces():获取object实现的实现接口类
     * object.getClass().getDeclaredMethods():获取object公开的方法
     */
}
